<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<jsp:include page="../../common/assets_header.jsp" />
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<link
	href="<%=request.getContextPath()%>/plugins/zTree/zTreeStyle/zTreeStyle.css"
	rel="stylesheet" type="text/css" />
<script src="<%=request.getContextPath()%>/assets/js/date-time/bootstrap-datepicker.min.js"></script>
<script src="<%=request.getContextPath()%>/assets/js/jqGrid/jquery.jqGrid.min.js"></script>
<script src="<%=request.getContextPath()%>/assets/js/jqGrid/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="../../plugins/zTree/jquery.ztree.core.js"></script>
<script type="text/javascript" src="../../plugins/zTree/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="../../plugins/zTree/jquery.ztree.exedit.js"></script>


<script type="text/javascript">
jQuery(document).ready(function() {
	var moduleMenuId="${moduleMenuId}";
	var moduleInfoId="${moduleInfoId}";
	loadGird(moduleMenuId); 
	
	if(moduleMenuId!=""&&moduleInfoId!=""){
		$("#listModuleInfos").val(moduleInfoId);
		getModulenfo(moduleInfoId);
		 var treeObj = $.fn.zTree.getZTreeObj("tree");
		 var sNodes = treeObj.getNodesByParam("moduleMenuId",moduleMenuId , null);
		 if (sNodes.length > 0) {
			 var  nodes=sNodes[0];
			 $("#"+nodes.tId+"_a").click();
		 } 
	}else{
		if(moduleInfoId==""){
			$("#listModuleInfos").val(-1);
		}else{
			$("#listModuleInfos").val(moduleInfoId);
			getModulenfo(moduleInfoId);
		}
	
	}
	<%-- $.ajax({
		type : "GET",
		url : "<%=request.getContextPath()%>/module/menu/isLeaf?id="+moduleMenuId,
		dataType : 'text',
		async : false,
		success : function(data) {
			if(data=="success"){
				
			}
		}	
	})  --%>
	
})


function add(){
		var moduleMenuId=$("#moduleMenuId").val();
		var moduleInfoId=$("#listModuleInfos").val();
	
		
		var isLastMenu=$("#isLastMenu").val();
		/* if(isLastMenu=="false"){
			$("#isLeaf").val(0);
			var isLeaf = 
		}else{ 
			$("#isLeaf").val(1);
		} */
		if($("#parentId").val() == "" || $("#moduleInfoId").val() !=moduleInfoId){
			$("#parentId").val(0);
		var	parentId=$("#parentId").val();
			location.href="<%=request.getContextPath()%>/module/menu/form?parentId="+parentId+"&moduleMenuId="+moduleMenuId+"&moduleInfoId="+moduleInfoId;
		}else{
			location.href="<%=request.getContextPath()%>/module/menu/form?parentId="+moduleMenuId+"&moduleInfoId="+moduleInfoId;
			<%-- $.ajax({
				type : "GET",
				url : "<%=request.getContextPath()%>/module/menu/isLeaf?id="+moduleMenuId,
				dataType : 'text',
				async : false,
				success : function(data) {
					if(data=="success"){
						
					}
				}	
			}) --%>
		}
		
		
	}
	
	function isLeaf(){
		var moduleMenuId=$("#moduleMenuId").val();
		var isLastMenu=$("#isLastMenu").val();
		if(isLastMenu=="false"){
			$.ajax({
				type : "GET",
				url : "<%=request.getContextPath()%>/module/menu/isLeaf?id="+moduleMenuId,
				dataType : 'text',
				async : false,
				success : function(data) {
					if(data=="success"){
						
					}
				}	
			})
			
		}
	}
	
	function del(){
		
		
		
		
		
		
		var isLastMenu=$("#isLastMenu").val();
		
		
		
		
		
		
		
		
		/* if(isLastMenu=="true"){
			noty({layout: 'center',text: "请选择子菜单",type: 'warning', timeout: 2000 });
		}else{ */
		 var id = $("#grid-table").jqGrid('getGridParam', 'selrow');
		 var obj = null;
		 if(id){
			 obj = $("#grid-table").jqGrid('getRowData', id);
		 }else{
			 bootbox.alert({  
		            buttons: {  
		               ok: {label: '确认',className: 'btn-ok'  }  
		            },  
		            message: '请选择一行数据.',  
		            title: "提示",  
		        });
			 return;
		}
	
		
		 $.ajax({
				type : "GET",
				url : "<%=request.getContextPath()%>/module/menu/listModules?id="+obj.moduleMenuId,
				dataType : 'text',
				async : false,
				success : function(data) {
					if(data=="success"){
						noty({layout: 'center',text: "请选择子菜单",type: 'warning', timeout: 2000 });
					}else{

						//发起异步请求，判断该菜单底下是否有权限
						$.ajax({
							type : "GET",
							url : "<%=request.getContextPath()%>/module/menu/isHasPur?id="+obj.moduleMenuId,
							dataType : 'text',
							async : false,
							success : function(data) {
								if(data=="isHasPur"){
									noty({layout: 'center',text: "该菜单有权限",type: 'warning', timeout: 2000 });
									/* $("#grid-table").delRowData($("#grid-table").jqGrid('getGridParam', 'selrow')); */
									
								}else{
									//在这里调删除
									
									 var susername = obj.menuName;
									 var id = obj.moduleMenuId;
									
									 var moduleMenuId=$("#moduleMenuId").val();
									 var moduleInfoId=$("#listModuleInfos").val();
									bootbox.dialog({
										message : "<spring:message code="sure you want to delete"></spring:message>"+susername+"<spring:message code="what"></spring:message>",
										title : "<spring:message code="delete the user"></spring:message>",
										buttons : {
											confirm : {
												label : '<spring:message code="sure to delete"></spring:message>',
												className : "btn red btn-ok",
												callback : function() {
													$.ajax({
														type : "GET",
														url : "<%=request.getContextPath()%>/module/menu/delete?id="+id,
														dataType : 'text',
														async : false,
														success : function(data) {
															if(data=="success"){
																$("#grid-table").delRowData($("#grid-table").jqGrid('getGridParam', 'selrow'));
																getModulenfo(moduleInfoId);
																 var treeObj = $.fn.zTree.getZTreeObj("tree");
																 var sNodes = treeObj.getNodesByParam("moduleMenuId",moduleMenuId , null);
																 if (sNodes.length > 0) {
																	 var  nodes=sNodes[0];
																	 $("#"+nodes.tId+"_a").click();
																	 isLeaf();
																 } 
																
															}else{
																noty({layout: 'center',text: "<spring:message code="delete the user failure"></spring:message>！",type: 'error', timeout: 2000 });
															}
														},
														error : function(data, status) {
															noty({layout: 'center',text: "<spring:message code="error"></spring:message>！",type: 'error', timeout: 2000 });
														}
													});
												}
											
											},
											cancel : {
												label : "<spring:message code="cancel"></spring:message>",
												className : "btn btn-danger"
											}
										}
									});
								}
							},
							error : function(data, status) {
								noty({layout: 'center',text: "<spring:message code="error"></spring:message>！",type: 'error', timeout: 2000 });
							}
						});
					}
				}	
			})
		 
		 
		 
	
	}
	//}
	
	function edit(){
		var id = $("#grid-table").jqGrid('getGridParam', 'selrow');
		
		var moduleMenuId=$("#moduleMenuId").val();
		var moduleInfoId=$("#moduleInfoId").val();
		//var moduleInfoId=$("#listModuleInfos").val();
		 var obj = null;
		if(moduleMenuId == 0){
			moduleInfoId=$("#listModuleInfos").val();
			 if(id){
				 obj = $("#grid-table").jqGrid('getRowData', id);
				 location.href="<%=request.getContextPath()%>/module/menu/form?id="+obj.moduleMenuId+"&moduleMenuId="+obj.moduleMenuId+"&moduleInfoId="+moduleInfoId;
			 }
		}else{
			 if(id){
				 obj = $("#grid-table").jqGrid('getRowData', id);
				 location.href="<%=request.getContextPath()%>/module/menu/form?id="+obj.moduleMenuId+"&moduleMenuId="+moduleMenuId+"&moduleInfoId="+moduleInfoId;
			 }
		}
		
		
		<%--  if(id){
			 obj = $("#grid-table").jqGrid('getRowData', id);
			 location.href="<%=request.getContextPath()%>/module/menu/form?id="+obj.moduleMenuId+"&moduleMenuId="+moduleMenuId+"&moduleInfoId="+moduleInfoId;
		 } --%>
		
	}

//初始化列表
function loadGird(id){
	if(id==""){
		id=-1;
	}
	var grid_selector = "#grid-table";//列表对象
	var pager_selector = "#grid-pager";//列表分页对象
	jQuery(grid_selector).jqGrid({
		url : "<%=request.getContextPath()%>/module/menu/list_json",
		datatype : "json",
		postData:{"id":id,"isMeun":true},
		height: 300,
		colNames:[/*' ',*/ ' ','menuName','menuTitle','isRoot','isLeaf','imagePath','menuPath','menuRemark','createUser'],
		colModel:[
		{name:'moduleMenuId',index:'moduleMenuId', width:60, sorttype:"int", hidden: true},
		{name:'menuName',index:'menuName',width:90,sortable:false},
		{name:'menuTitle',index:'menuTitle', width:90,sortable:false},
		{name:'isRoot',index:'isRoot', width:90,sortable:false},
		{name:'isLeaf',index:'isLeaf', width:90,sortable:false},
		{name:'imagePath',index:'imagePath', width:90,sortable:false},
		{name:'menuPath',index:'menuPath', width:90,sortable:false},
		{name:'menuRemark',index:'menuRemark', width:90,sortable:false},
		{name:'createUser',index:'createUser', width:90,sortable:false},
		], 
		viewrecords : true,
		rowNum:10,
		
		rowList:[10,20,30],
		pager : pager_selector,
		altRows: true,
		multiselect: false,
		 multiboxonly: true,
		 loadComplete : function() {
			},
		//editurl: $path_base+"/dummy.html",//nothing is saved
		caption: "Menu List",
		autowidth: true
	});
}


function getModulenfo(id){
	if(id==-1){
		return false;
	}
	$("#grid-table").clearGridData(false);
	//$("#purviewList").hide();
	$("#tree").innerHTML="";
	//模拟ajax请求
	 $.ajax({
		  type : "GET",
			url : "<%=request.getContextPath()%>/module/menu/listModuleInfos?id="+id,
			dataType : 'json',
			async : false,
			success : function(data) {
				var treeNodes =data;
				var setting={	
						data:{
							key:{
								name:"menuName",
								title:"menuName"
							},
							simpleData:{
								enable:true,
								idKey:"moduleMenuId",
								pIdKey:"parentId"
							},
							keep: {
								parent: true
							}
						},
						callback: {
							onClick: zTreeOnClick
						},
						async: {
							enable: true
						}
					}
				 $.fn.zTree.init($("#tree"), setting, treeNodes);
				 var treeObj = $.fn.zTree.getZTreeObj("tree");
				 treeObj.expandAll(true);
			}
	  });
}

//树形菜单点击事件
function zTreeOnClick(event, treeId, treeNode) {
	$("#moduleMenuId").val(treeNode.moduleMenuId);
	$("#moduleInfoId").val(treeNode.moduleInfoId);
	$("#parentId").val(treeNode.moduleMenuId);
	
	var moduleMenuId=treeNode.moduleMenuId;
	var isMeun = true;
	if(moduleMenuId==0){
		isMeun = false;
		reloadTable(treeNode.moduleInfoFlag,isMeun); 
	}else{
		reloadTable(treeNode.moduleMenuId,isMeun); 
	}
	
	var treeObj = $.fn.zTree.getZTreeObj("tree");
	var sNodes = treeObj.getSelectedNodes();
	
	
	if (sNodes.length > 0) {
		var level = sNodes[0].level;
		if(level==0||sNodes[0]!=null&&sNodes[0].children!=null&&sNodes[0].children.length!=null&&sNodes[0].children.length>0){
			$("#isLastMenu").val("true");
		}else{
			$("#isLastMenu").val("false");
		}
	}

	
}



//js动态生成列表
function  reloadTable(id,isMeun){
		if(isMeun == null){
			isMeun == true;
		}
		$("#grid-table").clearGridData(false);
		var postData = $("#grid-table").jqGrid("getGridParam", "postData");
		$.extend(postData, {"id":id,"isMeun":isMeun});
		
		$("#grid-table").trigger("reloadGrid");
	}
</script>
</head>
<body>
<div class="page-content">
		<div class="page-header">
			<button class="btn btn-xs btn-primary" onclick="add();" id="newBtn">New</button>
			<button class="btn btn-xs btn-primary" onclick="edit();" id="editBtn">Edit</button>
			 <button class="btn btn-xs btn-primary" onclick="del();" id="del">Delete</button>
			<input type="hidden" name="moduleMenuId" id="moduleMenuId" ></input>
			<input type="hidden" id="parentId" name="parentId" value="${moduleMenu.parentId}"> </input>
			<input type="hidden" id="moduleInfoId" name="moduleInfoId" value="${moduleMenu.moduleInfoId}"></input>
			<input type="hidden" id=moduleMenuId name="moduleMenuId" value="${moduleMenu.moduleMenuId}"></input>
			<input type="hidden" name="isLastMenu" id="isLastMenu" value="false"></input>	
			<input type="hidden" name="isLeaf" value="${moduleMenu.isLeaf}"></input>				     
		</div>


		<div class="row">
			<div class="col-sm-4">
			请选择模块：
				<select id="listModuleInfos" name="getModule" onchange="getModulenfo(this.value);">
						<option selected="selected" value="-1">---请选择模块---</option>
							<c:forEach items="${list }" var="item">
								<option value="${item.moduleInfoId }">${item.moduleName}</option>
						</c:forEach>
				</select>
				<div class="widget-box">
					<div class="widget-header widget-header-flat">
						<h4 class="smaller">
							 树形菜单
						</h4>
					</div>

					<div class="widget-body">
						<div class="widget-main">
							<div id="tree" class="ztree"></div>
						</div>
					</div>
				</div>
			</div>

			<div id="menuList"  class="col-sm-6">
				<div class="widget-box">
					<div class="widget-header widget-header-flat">
						<h4 class="smaller">
							菜单
						</h4>
					</div>

					<div class="widget-body">
						<div class="widget-main">
							
							<!-- 列表 -->
							<table id="grid-table"></table>
							<!-- 分页 -->
							<div id="grid-pager"></div>
							
							
						</div>
					</div>
				</div>
			</div>


		</div>


	</div>



	<script>


<%-- 

function delmoduleMenu(id,menuName){
	
	bootbox.dialog({
		message : "<spring:message code="sure you want to delete"></spring:message>"+menuName+"<spring:message code="what"></spring:message>",
		title : "<spring:message code="delete the user"></spring:message>",
		buttons : {
			confirm : {
				label : '<spring:message code="sure to delete"></spring:message>',
				className : "btn red btn-ok",
				callback : function() {
					$.ajax({
						type : "GET",
						url : "<%=request.getContextPath()%>/module/info/delete?id="+id,
						dataType : 'text',
						async : false,
						success : function(data) {
							if(data=="success"){
								//noty({layout: 'center',text: "删除用户成功！",type: 'success', timeout: 2000 });
								$("#list_form").attr("action","<%=request.getContextPath() %>/module/menu/list");
								$("#list_form").submit();
							}else{
								noty({layout: 'center',text: "<spring:message code="delete the user failure"></spring:message>！",type: 'error', timeout: 2000 });
							}
						},
						error : function(data, status) {
							noty({layout: 'center',text: "<spring:message code="error"></spring:message>！",type: 'error', timeout: 2000 });
						}
					});
				}
			},
			cancel : {
				label : "<spring:message code="cancel"></spring:message>",
				className : "btn btn-danger"
			}
		}
	});
}




function toEditPage(id){
	var sel = document.getElementById("listModuleInfos");
	var selected_val = sel.options[sel.selectedIndex].value;
	if($("#parentId").val() == "" || $("#moduleInfoId").val() != selected_val){
		alert("enter");
		$("#parentId").val(0);
	}
	if(id==null){
		//新增菜单
		$("#moduleInfoId").val(selected_val);
		$("#id").val();
		$("#menuName").val("");
		$("#menuTitle").val("");
		$("#menuRemark").val("");
		$("#moduleMenuForm").attr("action","<%=request.getContextPath()%>/module/menu/add");
	}else{
		//编辑菜单
		 $.ajax({
		type : "GET",
		url : "<%=request.getContextPath()%>/module/menu/form?id="+id,
		dataType : 'json',
		async : true,
		success : function(data) {
		$("#id").val(data.moduleMenuId);
		$("#menuName").val(data.menuName);
		 $("#menuTitle").val(data.menuTitle);	
		$("#menuRemark").val(data.menuRemark);
	}
})
		
		
	$("#moduleMenuForm").attr("action","<%=request.getContextPath()%>/module/menu/update");		
	}
	
	$("#moduleMenuDiv").modal('show');
	
}


function toListPage(pageIndex){
	$("#list_form").attr("action","<%=request.getContextPath() %>/module/menu/list");
	$("#iDisplayStart").val(pageIndex);
	$("#list_form").submit();
}


function saveOrEdit(){
	$.ajax({
        cache: true,
        type: "POST",
        url:$('#moduleMenuForm').attr('action'),
        data:$('#moduleMenuForm').serialize(),// 你的formid
        async: false,
        error: function(request) {
        	noty({layout: 'center',text: "数据处理是败",type: 'warning', timeout: 2000 });
        },
        success: function(data) {
        	
        	    //获取选中节点 并模拟单击该节点
        		var treeObj = $.fn.zTree.getZTreeObj("tree");
				var sNodes = treeObj.getSelectedNodes();
				
				if (sNodes.length > 0) {
					var  nodes=sNodes[0];
					$("#"+nodes.tId+"_a").click();
					getModulenfo(nodes.moduleInfoId);
					$("#"+nodes.tId+"_a").click();
						}
				//如果没有展示table.刷新整个树（相当于建立顶级）
				 if($("#list").is(":hidden")){
					var sel = document.getElementById("listModuleInfos");
					var selected_val = sel.options[sel.selectedIndex].value;
					getModulenfo(selected_val);
				}  
				
				$('#moduleMenuDiv').modal('hide');	
				
				}
    })
}


//树形菜单点击事件
function zTreeOnClick(event, treeId, treeNode) {
	$("#moduleMenuId").val(treeNode.moduleMenuId);
	$("#moduleInfoId").val(treeNode.moduleInfoId);
	
	$("#list").show();
	$.ajax({
		type : "GET",
		
		 url :  "<%=request.getContextPath()%>/module/menu/listModules?id="+treeNode.moduleMenuId,
		dataType : 'json',
		async : false,
		success : function(data) {
			var list_tr = "";
			$("#list tr.odd.gradeX").remove();
			data.forEach(function(moduleMenu){
				
				list_tr += "<tr class='odd gradeX'><td>"+moduleMenu.moduleMenuId+"</td> <td>"+moduleMenu.menuName+"</td>";
				list_tr +="<td><a href='#' onclick='toEditPage("+moduleMenu.moduleMenuId+");'class='btn mini blue'><spring:message code='btn.common.edit'></spring:message></a><a onclick='delmoduleMenu("+moduleMenu.moduleMenuId+",\""+moduleMenu.menuName+"\");' href='#' class='btn mini red'><spring:message code='btn.common.delete'></spring:message></a></td></tr>"
			 })
			 $("#list").append(list_tr); 
		},
		error : function(data, status) {
			noty({layout: 'center',text: "<spring:message code="error"></spring:message>！",type: 'error', timeout: 2000 });
		}
	});
}


 --%>
</script>




</body>
</html>





